< previous page page_57 next page >

Page 57
Puzzle 13
Registry Games, Part 3
Before moving on to enumerating values, let's review how to enumerate keys. The RegEnumKey function was shown earlier and appears like this in the Win32 API documentation:
LONG RegEnumKey(
    HKEY hKey,                  // handle of key to query
    DWORD dwIndex,              // index of subkey to query
    LPTSTR lpName,              // address of buffer for subkey name
    DWORD cbName                // size of subkey buffer
   );
The Visual Basic declaration is straightforward:
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias _
"RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _
lpName As String, ByVal cbName As Long) As Long
The EnumerateKeys function has been extended to perform the full enumeration. It uses the GetKeyInfo3 function to obtain the number of keys and necessary buffer length. The keybuffer variable is preinitialized to this length (plus an extra byte for the terminating NULL character) to prevent possible memory overwrites. The RegEnumKey function is called once for each subkey. On success, a new clsKeyValues object is created. This object is first loaded with the name of the subkey. The RegOpenKeyEx function is then used to open and obtain a handle to the subkey. This handle is passed to the EnumerateValues function, which returns another collection, this one containing a list of the values for the subkey. The subkey is then closed, and the clsKeyValues object added to the KeyCollection collection. This collection is returned as a result by the EnumerateKeys function, as follows:
' Enumerate all of the subkeys of this key, returning a collection
' containing clsKeyValues objects
Private Function EnumerateKeys(ByVal hKey As Long) As Collection
   Dim NumberOfSubKeys As Long
   Dim MaxSubKeyLength As Long
   Dim res As Long

 
< previous page page_57 next page >